# -*- coding: UTF-8 -*-
"""
Course:        CS408 Computer Science Project B, TEAM 1
Semester:      2010 SPRING
Project:       DOHN(Web-based Account Book System)

File:          sessions.py
Description:   Session Methods
Class:         None

Author:        SungHyuk Im
"""


from mod_python import Session as modPythonSession
import db


def __get_session_id__(req):
    """
    @author: SungHyuk Im
    @summary: If there exists a session, returns session user_id.
    Otherwise, returns False.
    @param req: request
    """
    sess = modPythonSession.Session(req)
    if sess.is_new():
        return False
    else:
        return sess["userid"]

def __set_session_id__(req, user_id, timeout = 3600):
    """
    @author: SungHyuk Im
    @summary: Sets session user_id and returns True.
    @param req: request
    @param user_id: user id
    @param pw: password
    @param timeout: amount of time to deactivate session
    """
    sess = modPythonSession.Session(req)
    sess["userid"] = user_id
    sess.set_timeout(timeout)
    sess.save()
    return True
    
def __erase_session_id__(req):
    """
    @author: SungHyuk Im
    @summary: Delete session and returns True
    @param req: request
    @return: True
    """
    sess = modPythonSession.Session(req)
    sess.invalidate()
    src = "sign out completed."
    return True

def sign_in(req, user_id, pw):
    """
    @author: SungHyuk Im
    @summary: Sign in
    @param req: request
    @param user_id: user input which HTML form sends through POST
    @param pw: user input which HTML form sends through POST
    @return: True
    
    @attention: Do not call __get_session_id__ or __set_session_id__
        twice in here. In the result, it takes no effect(same as before).
        This took me a lot of time to debug.
    """
    """
    # Check signed status
    # (This part will be deprecated. It is remained for test.)
    if check_signed(req):    # If already signed in
        saved_id = __get_session_id__(req)
        src = "<html><body>" + \
              "You are already signed in [ %s ].<br>" % (saved_id) + \
              "If you want to sign in with another account," + \
              " sign out first.<br><br> " + \
              "<form action = \"../view.py/sign_out\" method = \"POST\">" + \
              "<input type = \"submit\" text = \"sign out\">" + \
              "</form></body></html> "
        return src
    """
    
    # Check ID and password by accessing DB --------------------
    sql = "SELECT password FROM member_tb WHERE (user_id = '%s')" % (user_id);
    data = db.run_query(sql)
    if len(data) != 1:    # If not only one password
        return False
    if data[0][0] != pw:  # If password is incorrect
        return False
    
    # Set session --------------------
    __set_session_id__(req, user_id)
    
    """
    ##### FOR TEST
    src = "sign_in is completed in this Session class.\n" +
          "Typed id is %s\nTyped password is %s\n" % (user_id, pw) +
          "Signed in based on your input data. you can check by check_id()"
    return src
    """
    return True

def sign_out(req):
    """
    @author: SungHyuk Im
    @summary: Sign out
    @param req: request
    """
    return __erase_session_id__(req)

def check_signed(req):
    """
    @author: SungHyuk Im
    @summary: Checks whether signed in or not.
    @param req: request
    @return: True(Already signed in), False(Not signed in)
    """
    saved_id = __get_session_id__(req)
    if saved_id:
        return True
    else :
        return False

def get_id(req):
    """
    @author: SungHyuk Im
    @summary: Returns signed id.
    @param req: request
    @return: : user_id(if signed)
        False(if not signed)
    """
    return __get_session_id__(req)
